package com.gt.gt_securitytest.controller;


import com.gt.gt_securitytest.entity.MyUserDetails;
import jakarta.annotation.Resource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class HelloController {
    @GetMapping("/hello")
    public String hello(){
        //从security中获得从数据库中查找到的UserDetails
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        MyUserDetails principal = (MyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        return "SecurityContextHolder.getContext().getAuthentication().getName():"+name+"SecurityContextHolder.getContext().getAuthentication().getPrincipal().getPhone:"+principal.getPhone();

    }

    @GetMapping("/security/admin")
    public String helloAdmin(){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        MyUserDetails principal = (MyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        return "SecurityContextHolder.getContext().getAuthentication().getName():"+name+"SecurityContextHolder.getContext().getAuthentication().getPrincipal().getRoles:"+principal.getRoles();
    }
    @GetMapping("/security/user")
    public String helloUser(){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        MyUserDetails principal = (MyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        return "SecurityContextHolder.getContext().getAuthentication().getName():"+name+"SecurityContextHolder.getContext().getAuthentication().getPrincipal().getRoles:"+principal.getRoles();
    }
}
